import {createRouter, createWebHistory} from "vue-router";
import Home from "../views/Home.vue";

const routes = [
    {
        path: '/',
        redirect: '/customerData'
    }, {
        path: "/",
        name: "Home",
        component: Home,
        children: [
            {
                path: "/customerData",
                name: "customerData",
                meta: {
                    title: '数据统计(地域)'
                },
                component: () => import (
                /* webpackChunkName: "dashboard" */
                "../views/dataManage/CustomData.vue")
            }, {
                path: "/provincesales",
                name: "provincesales",
                meta: {
                    title: "省销售统计(地域)"
                },
                component: () => import (
                /* webpackChunkName: "charts" */
                "../views/dataManage/ProvinceSale.vue")
            }, {
                path: "/citysales",
                name: "citysales",
                meta: {
                    title: '市销售统计(地域)'
                },
                component: () => import (
                /* webpackChunkName: "form" */
                "../views/dataManage/CitySale.vue")
            }, {
                path: "/areasales",
                name: "areasales",
                meta: {
                    title: '区销售统计(地域)'
                },
                component: () => import (
                /* webpackChunkName: "tabs" */
                "../views/dataManage/AreaSale.vue")
            }, {
                path: "/provinceactive",
                name: "provinceactive",
                meta: {
                    title: '省活跃度统计(地域)'
                },
                component: () => import (
                /* webpackChunkName: "donate" */
                "../views/dataManage/ProvinceActive.vue")
            }, {
                path: "/cityactive",
                name: "cityactive",
                meta: {
                    title: '市活跃度统计(地域)',
                },
                component: () => import (
                /* webpackChunkName: "permission" */
                "../views/dataManage/CityActive.vue")
            }, {
                path: "/areaactive",
                name: "areaactive",
                meta: {
                    title: '区活跃度统计(地域)'
                },
                component: () => import (
                "../views/dataManage/AreaActive.vue")
            }, {
                path: "/cardcumbercatchcetting",
                name: "cardcumbercatchcetting",
                meta: {
                    title: '卡号批次设置'
                },
                component: () => import (
                "../views/card/CardManage.vue")
            }, {
                path: "/bigcustomerlist",
                name: "bigcustomerlist",
                meta: {
                    title: '大客户专员列表'
                },
                component: () => import (
                /* webpackChunkName: "icon" */
                "../views/bigcustomer/BigCustomerList.vue")
            },
            {
                path: "/setup",
                name: "setup",
                meta: {
                    title: '设置'
                },
                component: () => import (
                /* webpackChunkName: "icon" */
                "../views/bigcustomer/setup.vue")
            },
            {
                path: "/bigcustomerorderlist",
                name: "bigcustomerorderlist",
                meta: {
                    title: '订单列表'
                },
                component: () => import (
                "../views/bigcustomer/bigcustomerorderlist.vue")
            },
            {
                path: "/walletlist",
                name: "walletlist",
                meta: {
                    title: '钱包列表'
                },
                component: () => import (
                "../views/wallet/walletlist.vue")
            },
            {
                path: "/financialmanagementorderlist",
                name: "financialmanagementorderlist",
                meta: {
                    title: '订单列表'
                },
                component: () => import (
                "../views/wallet/financialmanagementorderlist.vue")
            },
            {
                path: '/:catchAll(.*)',
                name: '404',
                meta: {
                    title: '找不到页面'
                },
                component: () => import (/* webpackChunkName: "404" */
                '../views/404.vue')
            }
        ]
    }, {
        path: "/login",
        name: "Login",
        meta: {
            title: '登录'
        },
        component: () => import (
        /* webpackChunkName: "login" */
        "../views/Login.vue")
    },
];

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
});

router.beforeEach((to, from, next) => {
    document.title = `${to.meta.title} | vue-manage-system`;
    const role = localStorage.getItem('token');
    
    if (!role && to.path !== '/login') {
        next('/login');
    } else if (to.meta.permission) {
        
        role === 'admin'
            ? next()
            : next('/403');
    } else {
        next();
    }
});

export default router;